package com.hntxk.cqzd.common.util;

import com.hntxk.core.util.StringUtil;
import com.hntxk.cqzd.diacrisis.model.his.HisModel;
import com.hntxk.cqzd.lis.model.LisModel;
import com.hntxk.sp.dict.manager.DictManager;
import com.hntxk.sp.dict.model.Dict;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;

public class CalculateAge {

    //根据日期计算年龄
    public static int getAgeByBirth(Date birthDay) {
        int age = 0;
        Calendar cal = Calendar.getInstance();
        Calendar calBirDay = Calendar.getInstance();
        calBirDay.setTime(birthDay);
        if (cal.before(calBirDay)) { //出生日期晚于当前时间，无法计算
            throw new IllegalArgumentException(
                    "出生日期晚于当前时间，无法计算!");
        }
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前，年龄减一
            } else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        return age;
    }


    /**
     * 根据id和数据字典名字给id设置对应的name
     * @param id 需设置name的id字段
     * @param dicttypeid 字典名称
     * @return
     */
    public static String idToName(String id,String dicttypeid){
        if(StringUtil.isNotNull(id)){
            String[] strId =id.split(",");
            String strName="";
            for(String strOne : strId){
                //设置标本留存下拉name
                Dict strDict = DictManager.getInstance().getDict(dicttypeid, strOne);
                strName+=strDict.getText()+',';
            }
            strName=strName.substring(0, strName.length()-1);
            return strName;
        }
        return null;
    };
}
